package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.system.mapper.ReportMaterialDetailMapper;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.ReportMaterialDetailAddBo;
import com.xjy.system.domain.bo.ReportMaterialDetailQueryBo;
import com.xjy.system.domain.bo.ReportMaterialDetailEditBo;
import com.xjy.system.domain.ReportMaterialDetail;
import com.xjy.system.domain.vo.ReportMaterialDetailVo;
import com.xjy.system.service.IReportMaterialDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * VIEWService业务层处理
 *
 * @author xjy
 * @date 2022-03-27
 */
@Service
public class ReportMaterialDetailServiceImpl extends ServicePlusImpl<ReportMaterialDetailMapper, ReportMaterialDetail> implements IReportMaterialDetailService {

    @Override
    public ReportMaterialDetailVo queryById(Long projId){
        return getVoById(projId, ReportMaterialDetailVo.class);
    }

    @Override
    public TableDataInfo<ReportMaterialDetailVo> queryPageList(ReportMaterialDetailQueryBo bo) {
        PagePlus<ReportMaterialDetail, ReportMaterialDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ReportMaterialDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ReportMaterialDetailVo> queryList(ReportMaterialDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ReportMaterialDetailVo.class);
    }

    private LambdaQueryWrapper<ReportMaterialDetail> buildQueryWrapper(ReportMaterialDetailQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportMaterialDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjId() != null, ReportMaterialDetail::getProjId, bo.getProjId());
		lqw.between(bo.getBeginCreateTime() != null && bo.getEndCreateTime() != null,
			ReportMaterialDetail::getCreateTime ,bo.getBeginCreateTime(), bo.getEndCreateTime());
		lqw.eq(StrUtil.isNotBlank(bo.getMaterialId()), ReportMaterialDetail::getMaterialId, bo.getMaterialId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ReportMaterialDetailAddBo bo) {
        ReportMaterialDetail add = BeanUtil.toBean(bo, ReportMaterialDetail.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ReportMaterialDetailEditBo bo) {
        ReportMaterialDetail update = BeanUtil.toBean(bo, ReportMaterialDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ReportMaterialDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
